package top.tenebrous.yzl.im.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期时间处理工具类
 *
 * @autor: tylong
 * @date: 2022/1/11 15:38
 * @company: xy
 * @version: 1.0
 */
public class DateUtils {

    public static final String[] parsePatterns = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-M-dd HH:mm:ss", "yyyy-MM-dd", "yyyy-M-dd"};

    /**
     * @Fields 日期格式yyyy-MM-dd
     */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    /**
     * @Fields 日期格式yyyy-MM-dd HH:mm:ss
     */
    public static final String DATE_FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    /**
     * @Fields 日期格式yyyyMMddHHmmss
     */
    public static final String DATE_FORMAT_DATE_TIME1 = "yyyyMMddHHmmss";

    /**
     * @Fields DATE_FORMAT_DATE_TIME2 : 日期格式yyyyMMdd
     */
    public static final String DATE_FORMAT_DATE_TIME2 = "yyyyMMdd";

    /**
     * date转String
     *
     * @param date
     * @param format 转换格式,例:yyyy-MM-dd HH:mm:ss
     */
    public static String dateToString(Date date, String format) {
        if (StringUtils.isBlank(format)) {
            format = DATE_FORMAT_DATE_TIME;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * String转date
     *
     * @param date
     * @param format 转换格式,例:yyyy-MM-dd HH:mm:ss
     */
    public static Date stringToDate(String date, String format) {
        if (StringUtils.isBlank(format)) {
            format = DATE_FORMAT_DATE_TIME;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            return dateFormat.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static int MinDiff(Date date1, Date date2) {
        int i = (int) ((date1.getTime() - date2.getTime()) / 1000 / 60);
        return i;
    }

//    public static Date stringToDateArr(String date) {
//        try {
//            if (StringUtils.isBlank(date)) {
//                return null;
//            }
//            return org.apache.commons.lang3.D(date, parsePatterns);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    // 获得某天最大时间 2017-10-15 23:59:59
    public static Date getEndOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        ;
        LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
        return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    // 获得某天最小时间 2017-10-15 00:00:00
    public static Date getStartOfDay(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
        LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
        return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date DateConvert(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (date != null) {
            String s = sdf.format(date);
            try {
                return sdf.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String addDateBySecond(Date date, int second,String format) {
        try {
            long time = date.getTime();
            second = second * 1000;
            time += second;
            return dateToString(new Date(time), format);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String addDateByMinute(Date date, int minute) {
        try {
            long time = date.getTime();
            minute = minute * 60 * 1000;
            time += minute;
            return dateToString(new Date(time), DATE_FORMAT_DATE_TIME);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date addDateTimeByMinute(Date date, int minute) {
        try {
            long time = date.getTime();
            minute = minute * 60 * 1000;
            time += minute;
            return stringToDate(dateToString(new Date(time), DATE_FORMAT_DATE_TIME), null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Long addDateByDay(Date date, int day) {
        try {
            long time = date.getTime();
            day = day * 24 * 60 * 60 * 1000;
            time += day;
            return time;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate;
        Date endDate;
        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }

    public static List<String> initDateList(String start, String end) {
        List<String> dateList = new ArrayList<>();
        Date startDate = DateUtils.stringToDate(start, DateUtils.DEFAULT_DATE_FORMAT);
        dateList.add(DateUtils.dateToString(startDate, "MM/dd"));
        Date endDate = DateUtils.stringToDate(end, DateUtils.DEFAULT_DATE_FORMAT);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(startDate);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(endDate);
        // 测试此日期是否在指定日期之后
        while (endDate.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(DateUtils.dateToString(calBegin.getTime(), "MM/dd"));
        }
        return dateList;
    }

    public static Long getCurrentTimeMillis(){
        return System.currentTimeMillis();
    }

    public static String millisToDate(long millis,String format){
        Date date = new Date(millis);
        return dateToString(date,format);
    }

    /**
     * @description: 获取当月1号日期
     * @author: lihao
     * @date: 2022-11-17 14:38
     * @param: [date]
     * @return: java.lang.String
     **/
    public static String getFirstDateOfMonth(Date date) {
        String format = "yyyy-MM-01";
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        if (nowTime.getTime() == beginTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @description:
     * 将 MM-dd 转化为 Date
     *
     * @author: zhangya
     * @date: 2023/4/12 11:34
     * @param:
     * @return:
     **/
//    public static Date converDate(String time) {
//        String yyyy = dateToString(new Date(), DEFAULT_DATE_FORMAT);
//        Date date1 = stringToDateArr(yyyy + " "+time + ":00");
//        return date1;
//    }
    //得到今天剩余秒数
    public static int getLastSeconds(){
        Calendar calendar=Calendar.getInstance();
        // 得到今天 晚上的最后一刻 最后时间
        String last=getTime()+" 23:59:59";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // 转换为今天
            Date latDate=sdf.parse(last);
            // 得到的毫秒 除以1000转换 为秒
            return (int)(latDate.getTime()-System.currentTimeMillis())/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getTime(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(new Date());
    }
//    public static void main(String[] args) {
//        System.out.println(converDate("09:30"));
//    }

}
